module sdldee.video.overlay;

import sdldee.c.sdl;
import sdldee.video.exception;
import sdldee.video.videosurface;
import sdldee.video.rectangle;

enum OverlayFormat {
	YV12 = 0x32315659,
	IYUV = 0x56555949,
	YUY2 = 0x32595559,
	UYVY = 0x59565955,
	YVYU = 0x55595659
}

class Overlay {
	private SDL_Overlay* _overlay;

	this(SDL_Overlay* overlay) {
		if(_overlay == null)
			throw new VideoException("Null overlay");
		_overlay = overlay;
	}
	
	this(int width, int height, OverlayFormat format, VideoSurface display) {
		_overlay = SDL_CreateYUVOverlay(width, height, format, display.csurface);
		if(_overlay == null)
			throw new VideoException("Cannot create YUV overlay");
	}
	
	this(int width, int height, OverlayFormat format) {
		this(width, height, format, VideoSurface.videoSurface());
	}
	
	~this() {
		SDL_FreeYUVOverlay(_overlay);
	}
	
	public SDL_Overlay* coverlay() {
		return _overlay;
	}
	
	public OverlayFormat format() {
		return cast(OverlayFormat) _overlay.format;
	}
	
	public int w() {
		return _overlay.w;
	}
	
	public int h() {
		return _overlay.h;
	}
	
	public int planes() {
		return _overlay.planes;
	}
	
	public ubyte** pixels() {
		return _overlay.pixels;
	}
	
	public bool hardware() {
		return (_overlay.flags & 0x80000000) != 0;
	}
	
	public void lock() {
		if(SDL_LockYUVOverlay(_overlay) < 0)
			throw new VideoException("Unable to lock YUV overlay");
	}
	
	public void unlock() {
		SDL_UnlockYUVOverlay(_overlay);
	}
	
	public void display(Rectangle dstrect) {
		if(SDL_DisplayYUVOverlay(_overlay, dstrect.crect) < 0)
			throw new VideoException("Unable to display YUV Overlay");
	}	
}
